% =========================================================================
% This demo will generate caffe training command on clipbord.
%   After running the demo, the users should paste the training command to
%   the terminal command line.
% =========================================================================

clear;clc;
global bForceUpdate
bForceUpdate = true;
% bForceUpdate = false;
% ResultFolder = '%FSRCNN-VDSR(MSRA)-Scale(2)';
% ResultFolder = 'VDSR0-VDSR-Scale(4)';
% ResultFolder = 'VDSR-y-noskip-Scale(3)';
% ResultFolder = 'FSRCNN-Scale(3)';
% ResultFolder = 'VDSRx-VDSR-Scale(3)';
% ResultFolder = 'VDSR18-VDSR-Scale(3)';
% ResultFolder = 'VDSR03-VDSR-Scale(3)';
ResultFolder = 'VDSR03-VDSR-Scale(4)';

%%
cmd.caffe = '/home/wks/caffe/c1/caffe-master/build/tools/caffe';
cmd.solver_mode = 'gpu 0';

% cmd.weights = '/home/wks/SR-Works/%result/VDSR_lesslayer_gaussian-2/models/VDSR_lesslayer_gaussian-init.caffemodel';
% cmd.weights = '/home/wks/SR-Works/%result/VDSR_msra-1/models/VDSR_msra-init.caffemodel';
% cmd.weights = 'E:\Matlab\Work\DeepLearning\Caffe\mycaffe\%result\[VDSR_msra-1]-tuning2-[VDSR_y-1]\models\VDSR_lesslayer_gaussian-init.caffemodel';
% cmd.weights = '/home/wks/SR-Works/%result/reimple_VDSR-1/models/reimple_VDSR_c1_clip1_more_iter_132500.caffemodel';

% cmd.snapshot = '/home/wks/SR-Works/%result/[VDSR_gaussian-2]-tuning2-[VDSR_r_skip-1]/models/VDSR_r_skip_iter_11900.solverstate';

%% set train & test
Test.batch_size = 2;
Train.batch_size = 64;

DataSet = 'data291'; % DataSet = 'Set5';
Aug.angles = 1 : 4;
Aug.scales = .6 : .1 : .9;  % 0.9;

% DataSet = 'TrainImg';
% Aug.angles = 1;
% Aug.scales = .6;

% Aug.flip_dims = [1 2];
Aug.flip_dims = [];

Patch.scale = 4; % 3; % 4;

% Patch.xflow = 'Bicubic->I->FSRCNN_BATCH';
% Patch.xflow = 'Bicubic->Bicubic->VDSR0';

% Patch.xflow = 'Bicubic->Bicubic';
% Patch.Yflow = 'VDSR';

% Patch.xflow = 'Bicubic->Bicubic->VDSR1';
% Patch.xflow = 'Bicubic->Bicubic->VDSR18';
Patch.xflow = 'Bicubic->Bicubic->VDSR03';
Patch.yflow = 'VDSR';

% Patch.xflow = 'Bicubic->I';
% Patch.yflow = 'FSRCNN';

Patch.ysize = 41;
Patch.overlap = 1;

Train.files = srdata.loadH5File(DataSet, Aug, Patch, Train.batch_size, cmd.solver_mode);
Test.files = srdata.loadH5File('Set5', [], Patch, Test.batch_size, cmd.solver_mode);
srimg.h5imshow(Train.files);

%% set solver param
solv = VDSR.getSolv;
% solv.max_iter = 500; % 1000;
solv.display = 1000;
solv.snapshot = 2000;

%% set net param
% way = 'init';

% way = 'y-lessskip';
% way = 'r-lessskip';

% way = 'y-scale4';

way = 'y';
% way = 'y-noskip';
% way = 'r';
% way = 'y-skip';
% way = 'r-skip';

% way = 'y-lesslayer';
% way = 'y-lesslayer-skip';
% way = 'r-lesslayer';
% way = 'r-lesslayer-skip';

% net.kernel_size = 5 ;
net.kernel_size = 3;

net.weight_init_type = 'msra';
% net.weight_init_type = 'gaussian(0.001)';
net.result_path = srpath.getResultPath(ResultFolder);
net = VDSR.getNet(way, net);

%%
if strcmpi(way, 'init')
    mycaffe.genModel0(net, solv, cmd, Train, Test);
else
    [mynet, TrainingCmd] = mycaffe.genTrainingCmd(net, solv, cmd, Train, Test);
%     if isempty(mycaffe.get_caller)
%         system(TrainingCmd)
%     end
end
